"""
auth : ginger
    构造参数
        project_id 是传参获取
        其他需要的数据是配置文件获取 ( 参数使用逻辑请查看配置文件 )
"""
import time
import datetime
import json
from common.do_conf import get_conf_data_bool, get_conf_data_int, get_conf_data


def param_creator(project_id='83e6df74-a6b7-4429-a2e3-b6fb1a9f6ab1'):
    """
    生成请求参数 , project_id 是传参获取 , 其他数据是从配置文件获取 .
    """
    param = {
        'projectId': project_id,
        'filterType': 'And',
        'filters': [{'type': '', 'name': '', 'compType': 'input', 'isShow': False}],

        'xaxis': get_conf_data('xaxis', 'xaxis_value'),
        'yaxis': param_creator_yaxis(),

        'timeType': param_creator_time_type(),
        'timeFilter': {
            'timeRange': get_conf_data_int('time_type', 'time_range'),
            'timeRangeUnit': get_conf_data('time_type', 'time_range_unit')
        },
        'times': param_creator_times()
    }
    return param


def param_creator_yaxis():
    yaxis = []
    if get_conf_data_bool('yaxis', 'test_case'):
        yaxis.append('testCase')
    if get_conf_data_bool('yaxis', 'api_case'):
        yaxis.append('apiCase')
    if get_conf_data_bool('yaxis', 'scenario_case'):
        yaxis.append('scenarioCase')
    if get_conf_data_bool('yaxis', 'load_case'):
        yaxis.append('loadCase')
    return yaxis


def param_creator_time_type():
    if get_conf_data_bool('time_type', 'is_dynamic_time'):
        return 'dynamicTime'
    else:
        return 'fixedTime'


def param_creator_times(times_before=None, times_over=None):
    """
    姜配置文件中的时间获取到
    处理成 13 位的时间戳格式 ( 接口接收参数需要 13 位时间戳 )
    以列表返回 开始时间 和 结束时间
    """
    if not times_before:
        times_before = get_conf_data('time_type', 'times_before')
    if not times_over:
        times_over = get_conf_data('time_type', 'times_over')

    time_array_before = datetime.datetime.strptime(times_before, "%Y-%m-%d %H:%M:%S")
    date_stamp_before = str(int(time.mktime(time_array_before.timetuple())))
    data_microsecond_before = str("%06d" % time_array_before.microsecond)[0:3]
    date_stamp_before = date_stamp_before + data_microsecond_before

    time_array_over = datetime.datetime.strptime(times_over, "%Y-%m-%d %H:%M:%S")
    date_stamp_over = str(int(time.mktime(time_array_over.timetuple())))
    data_microsecond_over = str("%06d" % time_array_over.microsecond)[0:3]
    date_stamp_over = date_stamp_over + data_microsecond_over

    time_s = [date_stamp_before, date_stamp_over]

    return time_s


def get_time_by_timestamp(timestamp):
    """
    该方法仅为验证时间处理是否正确
    从配置文件获取到日期 ,
        调用 param_creator_times() 获得时间戳
        再调用 该方法 验证时间戳转换后确实为配置文件中的时间
    """
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp / 1000))


if __name__ == '__main__':
    print(param_creator_times())
    print(get_time_by_timestamp(eval(param_creator_times()[0])))
    print(get_time_by_timestamp(eval(param_creator_times()[1])))
    print(param_creator())
    print(json.dumps(param_creator()))
